package com.yupi.yuoj.controller;

import com.yupi.yuoj.common.BaseResponse;
import com.yupi.yuoj.common.ErrorCode;
import com.yupi.yuoj.common.ResultUtils;
import com.yupi.yuoj.exception.BusinessException;
import com.yupi.yuoj.model.dto.questionsubmit.QuestionSubmitAddRequest;
import com.yupi.yuoj.model.entity.ContestSubmit;
import com.yupi.yuoj.model.entity.User;
import com.yupi.yuoj.service.ContestSubmitService;
import com.yupi.yuoj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 竞赛提交接口
 */
@RestController
@RequestMapping("/contest_submit")
@Slf4j
public class ContestSubmitController {

    @Resource
    private ContestSubmitService contestSubmitService;

    @Resource
    private UserService userService;

    /**
     * 在竞赛中提交题目
     *
     * @param contestId
     * @param questionSubmitAddRequest
     * @param request
     * @return 提交 id
     */
    @PostMapping("/do")
    public BaseResponse<Long> doContestSubmit(Long contestId, @RequestBody QuestionSubmitAddRequest questionSubmitAddRequest,
            HttpServletRequest request) {
        if (contestId == null || contestId <= 0 || questionSubmitAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 提交题目
        long questionSubmitId = contestSubmitService.doContestSubmit(contestId, questionSubmitAddRequest, request);
        return ResultUtils.success(questionSubmitId);
    }

    /**
     * 获取用户在竞赛中某题的提交次数
     *
     * @param contestId
     * @param questionId
     * @param request
     * @return
     */
    @GetMapping("/attempts")
    public BaseResponse<Integer> getUserQuestionAttempts(Long contestId, Long questionId, HttpServletRequest request) {
        if (contestId == null || contestId <= 0 || questionId == null || questionId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        int attempts = contestSubmitService.getUserQuestionAttempts(contestId, questionId, loginUser.getId());
        return ResultUtils.success(attempts);
    }

    /**
     * 获取用户在竞赛中已解决的题目数
     *
     * @param contestId
     * @param request
     * @return
     */
    @GetMapping("/solved")
    public BaseResponse<Integer> getUserSolvedCount(Long contestId, HttpServletRequest request) {
        if (contestId == null || contestId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        int solvedCount = contestSubmitService.getUserSolvedCount(contestId, loginUser.getId());
        return ResultUtils.success(solvedCount);
    }

    /**
     * 获取用户在竞赛中的所有提交
     *
     * @param contestId
     * @param request
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<List<ContestSubmit>> getUserContestSubmits(Long contestId, HttpServletRequest request) {
        if (contestId == null || contestId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        List<ContestSubmit> submits = contestSubmitService.getUserContestSubmits(contestId, loginUser.getId());
        return ResultUtils.success(submits);
    }
} 